#include "redstone.h"

/* 
// 27k, 150k
// 电压下限
uint16_t RS_strength_mv_min[RS_STRENGTH_SIZE] = {
    [RS_STRENGTH_DISCONN] = 0,
    [RS_STRENGTH_0] = 2930, // 2.93 - 3.03
    [RS_STRENGTH_0_5] = 3050, // 3.08 - 3.18
    [RS_STRENGTH_1] = 3180, // 3.23 - 3.43
    [RS_STRENGTH_2] = 3500, // 3.56 - 3.80
    [RS_STRENGTH_3] = 3855, // 3.9 - 4.19
    [RS_STRENGTH_4] = 4250, // 4.25 - 4.58
    [RS_STRENGTH_5] = 4620, // 4.62 - 4.96
    [RS_STRENGTH_6] = 5010, // 5.00 - 5.35
    [RS_STRENGTH_7] = 5399, // 5.39 - 5.75
    [RS_STRENGTH_8] = 5799, // 5.79 - 6.17
    [RS_STRENGTH_9] = 6190, // 6.20 - 6.56
    [RS_STRENGTH_10] = 6615, // 6.61 - 6.96
    [RS_STRENGTH_11] = 7030, // 7.03 - 7.36
    [RS_STRENGTH_12] = 7460, // 7.46 - 7.75
    [RS_STRENGTH_13] = 7890, // 7.89 - 8.14
    [RS_STRENGTH_14] = 8250, // ? - 8.49
    [RS_STRENGTH_15] = 8650, // ? - 8.85
    [RS_STRENGTH_16] = 8900,
};
*/

/* 
// 56k, 330k
// 电压下限
uint16_t RS_strength_mv_min[RS_STRENGTH_SIZE] = {
    [RS_STRENGTH_DISCONN] = 0,
    [RS_STRENGTH_0] = 2900, //  - 3.83
    [RS_STRENGTH_0_5] = 3840, // 3.84 - 4.15
    [RS_STRENGTH_1] = 3970, // 3.97 - 4.16
    [RS_STRENGTH_2] = 4250, // 4.31 - 4.51
    [RS_STRENGTH_3] = 4570, // 4.59 - 4.85
    [RS_STRENGTH_4] = 4870, // 4.89 - 5.20
    [RS_STRENGTH_5] = 5200, // 5.20 - 5.55!
    [RS_STRENGTH_6] = 5540, // 5.53 - 5.89!
    [RS_STRENGTH_7] = 5870, // 5.86 - 6.25!
    [RS_STRENGTH_8] = 6230, // 6.20 - 6.59!
    [RS_STRENGTH_9] = 6575, // 6.55 - 6.94!
    [RS_STRENGTH_10] = 6925, // 6.91 - 7.29!
    [RS_STRENGTH_11] = 7285, // 7.28 - 7.63!
    [RS_STRENGTH_12] = 7650, // 7.65 - 7.96
    [RS_STRENGTH_13] = 8020, // 8.03 - 8.28
    [RS_STRENGTH_14] = 8400, // 8.41 - 8.58
    [RS_STRENGTH_15] = 8750, // 8.80 - 8.87
    [RS_STRENGTH_16] = 8900,
};
*/

// 11k, 56k
// 电压下限
uint16_t RS_strength_mv_min[RS_STRENGTH_SIZE] = {
    [RS_STRENGTH_DISCONN] = 0,
    [RS_STRENGTH_0] = 2190, // 2.19 - 2.29
    [RS_STRENGTH_0_5] = 2305, // 2.35 - 2.67
    [RS_STRENGTH_1] = 2410, // 2.49 - 2.69
    [RS_STRENGTH_2] = 2790, // 2.89 - 3.11
    [RS_STRENGTH_3] = 3195, // 3.28 - 3.54
    [RS_STRENGTH_4] = 3605, // 3.68 - 3.98
    [RS_STRENGTH_5] = 4050, // 4.11 - 4.42
    [RS_STRENGTH_6] = 4470, // 4.53 - 4.86
    [RS_STRENGTH_7] = 4910, // 4.96 - 5.31
    [RS_STRENGTH_8] = 5350, // 5.41 - 5.76
    [RS_STRENGTH_9] = 5805, // 5.86 - 6.22
    [RS_STRENGTH_10] = 6290, // 6.32 - 6.67
    [RS_STRENGTH_11] = 6750, // 6.79 - 7.11
    [RS_STRENGTH_12] = 7220, // 7.27 - 7.55
    [RS_STRENGTH_13] = 7700, // 7.76 - 7.99
    [RS_STRENGTH_14] = 8150, // 8.25 - 8.41
    [RS_STRENGTH_15] = 8650, // 8.75 - 8.81
    [RS_STRENGTH_16] = 8900,
};

// 27k, 150k
uint16_t RS_strength_mv_mid[RS_STRENGTH_SIZE] = {
    [RS_STRENGTH_DISCONN] = 0,
    [RS_STRENGTH_0] = 2980, // 2.93 - 3.03
    [RS_STRENGTH_0_5] = 3130, // 3.08 - 3.18
    [RS_STRENGTH_1] = 3330, // 3.23 - 3.43
    [RS_STRENGTH_2] = 3680, // 3.56 - 3.80
    [RS_STRENGTH_3] = 4045, // 3.9 - 4.19
    [RS_STRENGTH_4] = 4415, // 4.25 - 4.58
    [RS_STRENGTH_5] = 4790, // 4.62 - 4.96
    [RS_STRENGTH_6] = 5175, // 5.00 - 5.35
    [RS_STRENGTH_7] = 5570, // 5.39 - 5.75
    [RS_STRENGTH_8] = 5890, // 5.79 - 6.17
    [RS_STRENGTH_9] = 6380, // 6.20 - 6.56
    [RS_STRENGTH_10] = 6785, // 6.61 - 6.96
    [RS_STRENGTH_11] = 7195, // 7.03 - 7.36

    [RS_STRENGTH_12] = 7605, // 7.46 - 7.75
    [RS_STRENGTH_13] = 8015, // 7.89 - 8.14
    [RS_STRENGTH_14] = 8305, // ? - 8.49
    [RS_STRENGTH_15] = 8815, // ? - 8.85
    [RS_STRENGTH_16] = 9000,
};

RS_strength_t ReAL_get_strength_min(struct redstone_stu *rs){
    uint16_t rs_mv = rs->get_millivolt(rs);

    for(uint8_t i = 1; i < RS_STRENGTH_SIZE; i ++){
        if(rs_mv < RS_strength_mv_min[i]){
            return i-1;
        }
    }
    
    return RS_STRENGTH_16;
}

RS_strength_t ReAL_get_strength_mid(struct redstone_stu *rs){
    uint16_t rs_mv = rs->get_millivolt(rs);

    for(uint8_t i = 1; i < RS_STRENGTH_SIZE; i ++){
        if(rs_mv < RS_strength_mv_min[i]){
            if((rs_mv - RS_strength_mv_min[i-1]) > (RS_strength_mv_min[i] - rs_mv)){
                return i;
            }else {
                return i-1;
            }
        }
    }
    
    return RS_STRENGTH_16;
}

